#include<cstdio>
#include<cstring>
#include<map>
typedef long long hs_typ;
const hs_typ neginf=(1ll<<63);
int n;
char typ[8],addr[30];
std::map<hs_typ,int> book;
hs_typ prs(){
	int len=strlen(&addr[1]);
	int mh_pos;
	for(mh_pos=1;mh_pos<=len;mh_pos++){
		if(addr[mh_pos]==':')break;
	}
	if(mh_pos>=len)return neginf;
	if(addr[mh_pos-1]=='.')return neginf;
	bool st_0=true;
	hs_typ pos=0;
	for(int i=mh_pos+1;i<=len;i++){
		if(addr[i]<'0'||addr[i]>'9')return neginf;
		if(st_0&&i>1&&addr[i-1]=='0')return neginf;
		if(addr[i]!='0')st_0=false;
		pos=pos*10+addr[i]-'0';
		if(pos>65535)return neginf;
	}
	hs_typ num[5];
	int cnt=1;
	memset(num,0,sizeof(num));
	st_0=true;
	for(int i=1;i<mh_pos;i++){
		if(addr[i]<'0'||addr[i]>'9'){
			if(addr[i]=='.'){
				if(i==1||addr[i-1]=='.')return neginf;
				cnt++;
				if(cnt>4)return neginf;
				st_0=true;
				continue;
			}
			else return neginf;
		}
		if(st_0&&i>1&&addr[i-1]=='0')return neginf;
		if(addr[i]!='0')st_0=false;
		num[cnt]=num[cnt]*10+addr[i]-'0';
		if(num[cnt]>255)return neginf;
	}
	if(cnt<4)return neginf;
	return pos<<36|num[1]<<27|num[2]<<18|num[3]<<9|num[4];
}
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%s%s",&typ[1],&addr[1]);
		hs_typ res=prs();
		if(res==neginf){
			printf("ERR\n");
			continue;
		}
		if(!strcmp(&typ[1],"Server")){
			int &temp=book[res];
			if(temp)printf("FAIL\n");
			else{
				temp=i;
				printf("OK\n");
			}
		}
		else{
			if(book.find(res)==book.end()){
				printf("FAIL\n");
				continue;
			}
			else printf("%d\n",book[res]);
		}
	}
	return 0;
}
